package com.kzj.service.impl;

import com.kzj.common.FailMsgConstant;
import com.kzj.common.GlobalConstant;
import com.kzj.common.utils.VerifyUtil;
import com.kzj.myJar.common.result.Result;
import com.kzj.myJar.common.utils.NoteUtil;
import com.kzj.myJar.common.utils.ValidateCodeUtil;
import com.kzj.myJar.springboot.common.util.MailUtil;
import com.kzj.myJar.springboot.redis.RedisJsonUtil;
import com.kzj.service.CommonService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.concurrent.TimeUnit;

@Service
public class CommonServiceImpl implements CommonService {
    @Value("${app.auth-code.period-of-validity}")
    int authCodePeriodOfValidity;
    @Value("${app.auth-code.length}")
    int authCodeLength;
    @Override
    public void sendEmailAuthCode(String email) {
        Assert.isTrue(VerifyUtil.verifyEmail(email), FailMsgConstant.EMAIL_FORMAT_ERROR);
        String code = ValidateCodeUtil.generateVerifyNumberCode(authCodeLength);
        String key=GlobalConstant.EMAIL_KEY_PREFIX+email;
        RedisJsonUtil.set(key,code,authCodePeriodOfValidity, TimeUnit.MINUTES);
        MailUtil.sendMail(email, GlobalConstant.MAIL_SUBJECT,String.format(GlobalConstant.MAIL_CONTENT_TEMPLATE,code,authCodePeriodOfValidity));
    }

    @Override
    public void sendPhoneAuthCode(String phone) {
        Assert.isTrue(VerifyUtil.verifyPhone(phone), FailMsgConstant.PHONE_FORMAT_ERROR);
        String code = ValidateCodeUtil.generateVerifyNumberCode(authCodeLength);
        String key=GlobalConstant.PHONE_KEY_PREFIX+phone;
        RedisJsonUtil.set(key,code,authCodePeriodOfValidity, TimeUnit.MINUTES);
        NoteUtil.sendNote(phone,code);
    }
}
